--[[
	GMod Last Man Standing, a gamemode for Garry's Mod
    Copyright (C) 2012 Wickex

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]

if SERVER then
	AddCSLuaFile("shared.lua")
end

if CLIENT then
	SWEP.Slot = 0
	SWEP.Icon = ""
	SWEP.DrawCrosshair = false
end

SWEP.Base = "weapon_glms_base"

SWEP.Type = WEAPON_MELEE
SWEP.HoldType = "melee"

SWEP.ViewModelFOV = 64
SWEP.ViewModelFlip = false

SWEP.NextStrike = 0

SWEP.Primary.Damage = 20
SWEP.Primary.Delay = 1
SWEP.Primary.Recoil = 0
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Ammo = "none"
SWEP.Primary.Range = 75
SWEP.Primary.Knockback = 50
SWEP.Primary.Automatic = false

function SWEP:Initialize()
	self:SetWeaponHoldType(self.HoldType)
end

function SWEP:Precache()
	for k,v in pairs(self.Hit) do
		util.PrecacheSound(v)
	end
	
	for k,v in pairs(self.HitBody) do
		util.PrecacheSound(v)
	end
	
	for k,v in pairs(self.Miss) do
		util.PrecacheSound(v)
	end
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_IDLE)
	return true
end

function SWEP:PrimaryAttack()
	--if !self:CanPrimaryAttack() then return end
	--self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)

	--self:HitWeapon()
	
	if (CurTime() < self.NextStrike) then return end
	self.NextStrike = (CurTime() + self.Primary.Delay)
	timer.Simple(self.DelayT1, self.PerformAttackAnim, self)
	timer.Simple(self.DelayT2, self.HitWeapon, self)
	self.Owner:SetAnimation(PLAYER_ATTACK1)
	self:SendWeaponAnim(self.Animations[math.random(self.Animations.Count)])
end

function SWEP:HitWeapon()
	local trace = self.Owner:GetEyeTrace()
	if trace.HitPos:Distance(self.Owner:GetShootPos()) <= self.Primary.Range then
		if (trace.Entity:IsPlayer() or trace.Entity:IsNPC() or trace.Entity:GetClass() == "prop_ragdoll") then
			self.Owner:EmitSound(self.HitBody[math.random(1,#self.HitBody)])
			trace.Entity:SetVelocity(self.Owner:GetAimVector() * self.Primary.Knockback, 0)
		else
			local phys = trace.Entity:GetPhysicsObject()
			if IsValid(phys) then
				phys:SetVelocity(self.Owner:GetVelocity() + (self.Owner:GetAimVector() * self.Primary.Knockback))
			end
			self.Owner:EmitSound(self.Hit[math.random(1,#self.Hit)])
		end
		
		bullet = {}
		bullet.Num = 1
		bullet.Src = self.Owner:GetShootPos()
		bullet.Dir = self.Owner:GetAimVector()
		bullet.Spread = Vector(0, 0, 0)
		bullet.Tracer = 0
		bullet.Force = 1
		bullet.Damage = self.Primary.Damage
		self.Owner:FireBullets(bullet)
	else
		self.Weapon:EmitSound(self.Miss[math.random(1,#self.Miss)])
	end
end

function SWEP:PerformAttackAnim()
	self.Owner:SetAnimation(PLAYER_ATTACK1)
end

function SWEP:SecondaryAttack() return false end
function SWEP:Reload() return false end
function SWEP:CanPrimaryAttack() return true end
function SWEP:CanSecondaryAttack() return false end
function SWEP:Think() end